/**
 * Created by cxj on 2016/2/22.
 */
var grid;

var showgrid;

var bootbox_global;



//查看现金流水对象
function viewHistory(objid,datarow) {


    show_div_grid();

    var obj = grid.date_list[datarow];

    var columns = [
        {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

            return "<span>"+parseInt( (gridobj.current_page-1)*gridobj.page_rows+parseInt(datarow)+1)+"</span>";
        }},
        { title: '提交时间', dataIndex: 'create_time',},
        {
            title: '状态', dataIndex: 'type', dataSource: function (data, datarow, gridobj, current_column) {

            var type = data[datarow].type;

            return getBankCashName(type);



        }
        },
        { title: '金额', dataIndex: 'amount',},
        { title: '总额(当时余额)', dataIndex: 'balance',},
        { title: '提现中(当时余额)', dataIndex: 'balance_withdrawing',},
        { title: '可用余额(当时余额)', dataIndex: 'balacan_cash',},
        //{ title: '原因', dataIndex: 'reason',},
        //{ title: '操作人', dataIndex: 'created_by',},


    ];

    grid_show = new Grid("/UserAmount/getBankCashList", $("#table_my_toast1"), $("#paging_my_toast1"), 1, 10, columns, "&user_id="+obj.user_id, 10);

    //初始化
    grid_show.Init();



    return;



}


//同意对象
function acceptObj(objid,datarow) {

    bootbox = bootbox_global;

    var obj = grid.date_list[datarow];

    //alert(roleid);

    var alertarray = {
        form: {api: "/UserAmount/acceptRequest", name: "alertform", id: "alertform"},
        inputs: [
            {title: "原因", type: "text", placeholder: "", verify: "", name: "reason", id: "reason"}
        ]

    }
    bootbox.dialog({
        title: "要同意该请求",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&request_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });





}

//拒绝对象
function denyObj(objid,datarow) {

    bootbox = bootbox_global;

    var obj = grid.date_list[datarow];

    //alert(roleid);

    var alertarray = {
        form: {api: "/UserAmount/denyRequest", name: "alertform", id: "alertform"},
        inputs: [
            {title: "原因", type: "text", placeholder: "", verify: "empty", name: "reason", id: "reason"}
        ]

    }
    bootbox.dialog({
        title: "拒绝该请求",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&request_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });





}

//查看操作日志对象
function viewObj(objid,datarow) {

    bootbox = bootbox_global;

    var obj = grid.date_list[datarow];


    //发送请求
    send_request_direct("/UserAmount/getLogList", "request_id="+objid, function process_data(data) {


        //  alert(data.data.data.length);

        var columns = [
            {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                return "<span>"+(parseInt(datarow)+1)+"</span>";
            }},
            { title: '提交时间', dataIndex: 'create_time',},
            {
                title: '状态', dataIndex: 'gender', dataSource: function (data, datarow, gridobj, current_column) {

                var obj = data[datarow];

                if(obj.status == "submiting")
                    return "<span class='label-default'>提交中</span>";


                if(obj.status == "accepted")
                    return "<span class='label-success'>审核成功<span class='glyphicon glyphicon-info-sign' aria-hidden='true'></span></span>";

                if(obj.status == "failed" || obj.status == "FAILED")
                    return "<span class='label-danger'>审核失败<span class='glyphicon glyphicon-info-sign' aria-hidden='true'></span></span>";

                return "";


            }
            },
            { title: '原因', dataIndex: 'reason',},
            { title: '操作人', dataIndex: 'created_by',},


        ];

        var message = getGridInfo(data.data.data,columns);

        bootbox_content_alert(message);


    }, function process_fail(data) {

        //操作失败
        AlertError(bootbox, data.msg);


    });

}




define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    $(function () {

        bootbox_global = bootbox;
        //每列数据列性
        var columns = [
            {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                return "<span>"+parseInt( (gridobj.current_page-1)*gridobj.page_rows+parseInt(datarow)+1)+"</span>";
            }},
            { title: '请求id', dataIndex: 'request_id',},
            { title: '手机号', dataIndex: 'mobile',},
            {title: '姓名', dataIndex: 'real_name',},
            {title: '银行', dataIndex: 'bank_name',},
            {title: '开户行', dataIndex: 'open_bank',},
            {title: '卡号', dataIndex: 'backcard_no',},
            {title: '提现金额', dataIndex: 'amount',},
            {title: '手续费', dataIndex: 'fee',},
            {title: '原因', dataIndex: 'reason',},
            {
                title: '状态', dataIndex: 'gender', dataSource: function (data, datarow, gridobj, current_column) {

                var obj = data[datarow];

                if(obj.status == "submiting")
                    return "<span class='label-default'>提交中</span>";


                if(obj.status == "accepted")
                    return "<span class='label-success'>审核成功<span class='glyphicon glyphicon-info-sign' aria-hidden='true'></span></span>";

                if(obj.status == "failed" || obj.status == "FAILED")
                    return "<span class='label-danger'>审核失败<span class='glyphicon glyphicon-info-sign' aria-hidden='true'></span></span>";

                return "";


            }
            },

            { title: '提交时间', dataIndex: 'create_time',},
            { title: '最后更新时间', dataIndex: 'update_time',},
            {title : '操作',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                var is_verify = data[datarow].status;


                var  html =  "<div class='div_btn'><button onclick=\"acceptObj("+data[datarow].request_id+","+datarow+")\" class='btn btn-info'>同意</button> <button onclick=\"denyObj("+data[datarow].request_id+")\"  class='btn btn-danger'>拒绝</button>";






                if(is_verify == "submiting")
                {
                    html =  html+"<div class='div_btn'><button onclick=\"viewObj("+data[datarow].request_id+","+datarow+")\" class='btn btn-success'>操作日志</button>";


                }
                else
                {
                    html = "<div class=''><button onclick=\"viewObj("+data[datarow].request_id+","+datarow+")\" class='btn btn-success'>操作日志</button>";

                }

                //log


                html =  html+"<div class='div_btn'><button onclick=\"viewHistory("+data[datarow].request_id+","+datarow+")\" class='btn btn-info'>流水日志</button>";

                html = html+"</div>"

                return html;
            }},




        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/userAmount/getList", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param="mobile="+$("#mobile").val()+"&status="+$("#status").val();


            return param;

        }
        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });
    });


});